package com.fc.soapparseweather;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String NAMESPACE = "http://WebXml.com.cn/";
	private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
	private static final String METHOD_NAME = "getWeatherbyCityName";
	private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyC-ityName";
	private String weatherToday;
	private Button okButton;
	private SoapObject detail;
	private EditText cityNameText;
	private TextView cityMsgView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		cityNameText = (EditText) findViewById(R.id.cityName);
		okButton = (Button) findViewById(R.id.ok);
		cityMsgView = (TextView) findViewById(R.id.textView1);
		okButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.e("--->>>fengchuan", "click------");
				new showWeatherAsyncTask().execute();
			}
		});
	}

	private class showWeatherAsyncTask extends AsyncTask<String, Integer, String>{

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			Log.e("--->>>fengchuan", "doInBackground------");
			showWeather();
			return null;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			Log.e("--->>>fengchuan", "onPreExecute------");
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
//			super.onPostExecute(result);
			Log.e("--->>>fengchuan", "onPostExecute------");
		}
		
	}
	private void showWeather(){
		String city = cityNameText.getText().toString().trim();
		Log.e("---->>>fengchuan", "------city = " + city);
		if(!city.isEmpty()){
			Log.e("--->>>fengchuan", "showWeather------");
			getWeather(city);
		}
	}
	private void getWeather(String cityName) {
		// TODO Auto-generated method stub
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			rpc.addProperty("theCityName", cityName);
			HttpTransportSE ht = new HttpTransportSE(URL);
			ht.debug = true;
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			Log.e("--->>>>fengchuan", "-----getWeather");
			ht.call(SOAP_ACTION, envelope);
//			SoapObject result = (SoapObject) envelope.bodyIn;
			SoapObject result = (SoapObject) envelope.bodyIn;
			Log.e("--->>>>fengchuan", "-----result = " + result);
			detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
			System.out.print("detail = " + detail);
			Log.e("--->>>>fengchuan", "-----detail = " + detail);
			parseWeather(detail);
		} catch (HttpResponseException e) {
			// TODO Auto-generated catch block
			Log.e("--->>>>fengchuan", "-----HttpResponseException");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("--->>>>fengchuan", "-----IOException");
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			Log.e("--->>>>fengchuan", "-----XmlPullParserException");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void parseWeather(SoapObject detail2) throws UnsupportedEncodingException{
		// TODO Auto-generated method stub
		String date = detail.getProperty(6).toString();
		weatherToday = "today: " + date.split(" ") [0];
		weatherToday = weatherToday + "weather: " + date.split(" ") [1];
		weatherToday = weatherToday + "temperature " + detail2.getProperty(5).toString();
		weatherToday = weatherToday + "wind power " + detail2.getProperty(7).toString();
		System.out.print("weathertoday = " + weatherToday);
		cityMsgView.setText(weatherToday);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
